<?php

class UserController extends Controller
{
	private $model;
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			)
		);
	}
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
	public function actionIndex()
	{

		if(isset($_POST['User']))
		{
			/*$_POST['User']['email'] = 'hoangl2nn@fpt.edu.vn';
			$_POST['User']['birthday'] = '19/11/1990';
			echo "<pre>";
			print_r($_POST);
			echo "</pre>";*/
			$this->model= new User;
			$this->model->attributes= $_POST['User'];
			switch ($_POST['yt0']) {
				case 'Đăng ký':
					echo $this->add();
					break;
			}
		}
	}

	public function add()
	{
		if(($this->model->validate()))
		{
			//Hash password
			$password = $this->model->password;
			$hash = CPasswordHelper::hashPassword($password);
			$this->model->setAttribute('password',$hash);

			$check = $this->model->add();
			if($check)
			{
				$_POST['LoginForm']['email']    = $this->model->email;
				$_POST['LoginForm']['password'] = $password;
				Yii::app()->runController('Authenticate/index');
				//return 'Đăng ký thành công';
			}
			else
			{
				return 'Đăng ký thất bại vui lòng kiểm tra lại.';
			}
		}
		else
		{
			$erros = $this->model->getErrors();
			$notification = '';
			foreach ($erros as $key)
			{
				$notification .= $key[0].', ';
			}
			return "$notification";
		}
	}

	public function view($id)
	{
		$user = User::model()->view($id);

		$user['birthday'] = date("d/m/Y", strtotime($user['birthday']));
		$user['sex'] = ($user['sex'] == '1' ? 'Nam' : 'Nữ');
		$user['other'] = json_decode($user['other']);
		return $user;
	}

	public function actionTest()
	{
		echo "Testing";
		Yii::app()->runController('Authenticate/hello2');
		//Yii::import('application.controllers.back.AuthenticateController');
		//$obj =new AuthenticateController(); // preparing object
		//$obj->hello();
	}
}